﻿Imports Microsoft.DirectX

Namespace Editor

    Module mEditor

        Private cDebugTextColor As Color = Color.Black
        Private cDebugTextBdrColor As Color = Color.White

        Private cGridColor As Color = Color.DarkOrange

        Public Const winWidth As Integer = 1024
        Public Const winHeight = 768

        Public Const TileWidth As Integer = 48
        Public Const TileHeight As Integer = 32

        Public Const DrawAreaCentreX As Single = 0.5
        Public Const DrawAreaCentreY As Single = 0.25
        Public Const DrawAreaFactorX As Single = 1.3
        Public Const DrawAreaFactorY As Single = 10
        Public DrawAreaStartOffsetX As Integer, DrawAreaStartOffsetY As Integer
        Public DrawAreaTilesX As Integer, DrawAreaTilesY As Integer
        Public DrawCentreTileX As Integer, DrawCentreTileY As Integer

        'DrawOffset             
        Dim offsetX As Integer
        Dim offsetY As Integer

        'CENTRE COORDS
        Public CoordX As Integer, CoordY As Integer

        'Mouse
        Private pointerdirection As Direction
        Public MouseX As Integer, MouseY As Integer
        Public MousePointX As Integer, MousePointY As Integer

        Public mCurrentMapFilename As String
        Public mCurrentMap As cMirMapFile
        Public mCurrentMapName As String

        Private WithEvents tmrMoveX As New System.Windows.Forms.Timer
        Private WithEvents tmrMoveY As New System.Windows.Forms.Timer

        Private MoveDistX As Integer
        Private MoveDistY As Integer

        Private tilex As Integer
        Private tiley As Integer

        Private MenuTileX As Integer
        Private MenuTileY As Integer

        Friend arrZoom() As Single = {1.0F, 0.9F, 0.8F, 0.7F, 0.6F, 0.5F, 0.4F, 0.3F, 0.2F, 0.1F}
        Friend ZoomIndex As Integer

        Private arrGrid() As Integer = {1, 2, 4, 8, 16, 32, 64}
        Private GridIndex As Integer
        Private GridVisible As Boolean = True

        Private WithEvents mWinEditor As GUI.MirWindow

        Private WithEvents mWinEditorMenu As GUI.MirWindow

        Private WithEvents mBtnAddSpawn As GUI.MirButton
        Private WithEvents mBtnAddNPC As GUI.MirButton
        Private WithEvents mBtnAddSZ As GUI.MirButton
        Private WithEvents mBtnAddDoor As GUI.MirButton

        Private WithEvents mBtnEditSpawn As GUI.MirButton
        Private WithEvents mBtnEditNPC As GUI.MirButton
        Private WithEvents mBtnEditSZ As GUI.MirButton
        Private WithEvents mBtnEditDoor As GUI.MirButton

        Private WithEvents mBtnRemoveSpawn As GUI.MirButton
        Private WithEvents mBtnRemoveNPC As GUI.MirButton
        Private WithEvents mBtnRemoveSZ As GUI.MirButton
        Private WithEvents mBtnRemoveDoor As GUI.MirButton

        Private WithEvents mBtnToggleGrid As GUI.MirButton
        Private WithEvents mBtnToggleGrid2 As GUI.MirButton

        Private WithEvents mBtnZoomOut As GUI.MirButton
        Private WithEvents mBtnZoomIn As GUI.MirButton

        Private WithEvents mBtnToggleSymbols As GUI.MirButton
        Public bShowSymbols As Boolean = False

        Private Const cWilIdx_ZoomOut As Integer = 9
        Private Const cWilIdx_ZoomIn As Integer = 10
        Private Const cWilIdx_ToggleSymbols As Integer = 11

        Public Const cWilIdx_SafeZone As Integer = 0
        Public Const cWilIdx_Mob As Integer = 1
        Public Const cWilIdx_NPC As Integer = 2
        Public Const cWilIdx_Door As Integer = 3

        Public Sub Init()

            ZoomIndex = 0

            CalculateDrawingArea()

            mWinEditor = New GUI.MirWindow("EditorWin", , , winWidth, winHeight)

            Windows.AddWindow(mWinEditor, Windows.WindowType.AlwaysBottom)

            mWinEditorMenu = New GUI.MirWindow("EditorMenuWin", , , 128, 80)
            
            mBtnAddSpawn = New GUI.MirButton(mWinEditorMenu, "BtnAddSpawn", 0, 0, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Add Spawn")
            mBtnAddNPC = New GUI.MirButton(mWinEditorMenu, "BtnAddNPC", 0, 16, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Add NPC")
            mBtnAddSZ = New GUI.MirButton(mWinEditorMenu, "BtnAddSZ", 0, 32, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Add Safe Zone")
            mBtnAddDoor = New GUI.MirButton(mWinEditorMenu, "BtnAddDoor", 0, 48, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Add Door")
            mBtnToggleGrid = New GUI.MirButton(mWinEditorMenu, "BtnToggleGrid", 0, 64, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Toggle Grid")

            mBtnEditSpawn = New GUI.MirButton(mWinEditorMenu, "BtnEditSpawn", 0, 0, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Edit Spawn")
            mBtnEditNPC = New GUI.MirButton(mWinEditorMenu, "BtnEditNPC", 0, 0, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Edit NPC")
            mBtnEditSZ = New GUI.MirButton(mWinEditorMenu, "BtnEditSZ", 0, 0, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Edit Safe Zone")
            mBtnEditDoor = New GUI.MirButton(mWinEditorMenu, "BtnEditDoor", 0, 0, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Edit Door")

            mBtnRemoveSpawn = New GUI.MirButton(mWinEditorMenu, "BtnRemoveSpawn", 0, 16, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Remove Spawn")
            mBtnRemoveNPC = New GUI.MirButton(mWinEditorMenu, "BtnRemoveNPC", 0, 16, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Remove NPC")
            mBtnRemoveSZ = New GUI.MirButton(mWinEditorMenu, "BtnRemoveSZ", 0, 16, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Remove Safe Zone")
            mBtnRemoveDoor = New GUI.MirButton(mWinEditorMenu, "BtnRemoveDoor", 0, 0, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Edit Door")

            mBtnToggleGrid2 = New GUI.MirButton(mWinEditorMenu, "BtnToggleGrid2", 0, 32, 128, 16, 0.01F, Color.DarkGray, Color.Gray, Color.LightGray, Color.White, Color.Black, "Toggle Grid")

            mBtnZoomOut = New GUI.MirButton(mWinEditor, "BtnZoomOut", winWidth - 80, 16, 64, 64, 0.01F, Color.Transparent, Color.Transparent, Color.Transparent, Color.Transparent, Color.Transparent, , False)
            mBtnZoomIn = New GUI.MirButton(mWinEditor, "BtnZoomIn", winWidth - 80, 96, 64, 64, 0.01F, Color.Transparent, Color.Transparent, Color.Transparent, Color.Transparent, Color.Transparent, , False)

            mBtnToggleSymbols = New GUI.MirButton(mWinEditor, "BtnToggleSymbols", winWidth - 80, 176, 64, 64, 0.01F, Color.Transparent, Color.Transparent, Color.Transparent, Color.Red, Color.Transparent, , False)

            tmrMoveX.Interval = 100
            tmrMoveY.Interval = 100

            tmrMoveX.Enabled = False
            tmrMoveY.Enabled = False

            CoordX = Int(mCurrentMap.mWidth / 2)
            CoordY = Int(mCurrentMap.mHeight / 2)

            NPC.LoadMapNPCs()
            Mobs.LoadMapMobs()
            'TODO: (Editor) LOad safezones
            'TODO: (Editor) LOad doors

        End Sub

        Private Function DebugText() As String

            Dim str As String = ""

            str = str & "     Menu Coords: " & MenuTileX & ":" & MenuTileY & vbCrLf
            str = str & "   Center Coords: " & CoordX & ":" & CoordY & vbCrLf
            str = str & "      Zoom Level: " & ZoomIndex

            Return str

        End Function

        Private Function InfoText() As String

            Dim str As String = "Map: " & mCurrentMapFilename & ".map (" & mCurrentMapName & ")    Grid Size: " & arrGrid(GridIndex)
            Return str

        End Function

        Public Sub CalculateDrawingArea()

            Dim w As Integer, h As Integer

            w = (winWidth * DrawAreaFactorX) / arrZoom(ZoomIndex)
            h = (winHeight * DrawAreaFactorY) / arrZoom(ZoomIndex)

            Dim r As Integer
            DrawAreaTilesX = Math.DivRem(w, TileWidth, r)
            If r = 0 Then DrawAreaTilesX += 1
            DrawAreaTilesY = Math.DivRem(h, TileHeight, r)
            If r = 0 Then DrawAreaTilesY += 1

            'Make sure there is an odd number of tiles in each dimension so we get a centre tile for the player
            Math.DivRem(DrawAreaTilesX, 2, r)
            If r = 0 Then DrawAreaTilesX += 1
            Math.DivRem(DrawAreaTilesY, 2, r)
            If r = 0 Then DrawAreaTilesY += 1

            Dim cx As Double = (winWidth / arrZoom(ZoomIndex) / 2) - (TileWidth / 2)
            Dim cy As Double = (winHeight / arrZoom(ZoomIndex) / 2) - (TileHeight / 2)

            DrawCentreTileX = DrawAreaTilesX * DrawAreaCentreX
            DrawCentreTileY = DrawAreaTilesY * DrawAreaCentreY

            'DrawAreaStartOffsetX = cx - ((DrawAreaTilesX \ 2) * Constants.TileWidth) - Constants.TileWidth
            'DrawAreaStartOffsetY = cy - ((DrawAreaTilesY \ 2) * Constants.TileHeight) - Constants.TileHeight
            DrawAreaStartOffsetX = cx - ((DrawCentreTileX) * TileWidth) '- Constants.TileWidth
            DrawAreaStartOffsetY = cy - ((DrawCentreTileY) * TileHeight) '- Constants.TileHeight

        End Sub

        Public Sub Shutdown()

            Main.frmEditor.Hide()

            Main.frmEditor = Nothing

            Windows.RemoveWindow(mWinEditor)

        End Sub

        Public Sub Show()

            If Main.frmEditor Is Nothing Then
                Main.frmEditor = New fEditor
            End If

            Main.frmEditor.Show()
            Main.frmEditor.Activate()

            Main.frmEditor.StartEngine()

        End Sub

        Private Sub mWinEditor_DrawWindow(ByRef mode As DirectX.mDirectX.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles mWinEditor.DrawWindow

            'On Error Resume Next

            If mCurrentMap Is Nothing Then Exit Sub

            offsetX = DrawAreaStartOffsetX
            offsetY = DrawAreaStartOffsetY

            Dim starttilex As Integer = CoordX - DrawCentreTileX
            Dim starttiley As Integer = CoordY - DrawCentreTileY

            Static tmps As SmallTile
            Static tmpl As LargeTile

            Static pX As Double, pY As Double
            Static currtilex As Integer
            Static currtiley As Integer
            Static tmpOffY As Single

            Dim tmpImg As MirImage

            z = 0.99

            Dim r As Integer

            If mode = DirectX.DrawingMode.Normal Then

                With Libs.Editor
                    .Image(cWilIdx_ZoomOut).Draw(winWidth - 80, 16, 0.01F)
                    .Image(cWilIdx_ZoomIn).Draw(winWidth - 80, 96, 0.01F)
                    .Image(cWilIdx_ToggleSymbols).Draw(winWidth - 80, 176, 0.01F)
                End With

                pY = offsetY
                currtiley = starttiley
                Math.DivRem(currtiley, 2, r)
                If r = 1 Then
                    'If pY + (TileHeight * arrZoom(ZoomIndex)) = pY Then pY += 1 Else pY += (TileHeight * arrZoom(ZoomIndex))
                    pY += TileHeight
                    currtiley += 1
                End If

                For tileY As Integer = 0 To DrawAreaTilesY Step 2
                    z -= 0.001
                    pX = offsetX
                    currtilex = starttilex
                    Math.DivRem(currtilex, 2, r)
                    If r = 1 Then
                        'If pX + (TileWidth * arrZoom(ZoomIndex)) = pX Then pX += 1 Else pX += (TileWidth * arrZoom(ZoomIndex))
                        pX += TileWidth
                        currtilex += 1
                    End If


                    For tileX As Integer = 0 To DrawAreaTilesX Step 2


                        tmpl = mCurrentMap.GetLargeTile(currtilex, currtiley)

                        If tmpl.TileSet <> 255 Then
                            tmpImg = Libs.GetLib(GetImgLib(tmpl.TileSet)).Image(tmpl.Index)
                            If Not tmpImg Is Nothing Then
                                With tmpImg

                                    DirectX.BlitD3D(.Texture, pX, pY, .Header.Width, .Header.Height, z, arrZoom(ZoomIndex))

                                End With
                            End If
                        End If
                        'If pX + ((TileWidth * arrZoom(ZoomIndex)) * 2) = pX Then pX += 1 Else pX += ((TileWidth * arrZoom(ZoomIndex)) * 2)
                        pX += TileWidth * 2
                        currtilex += 2
                    Next
                    'If pY + ((TileHeight * arrZoom(ZoomIndex)) * 2) = pY Then pY += 1 Else pY += ((TileHeight * arrZoom(ZoomIndex)) * 2)
                    pY += TileHeight * 2
                    currtiley += 2
                Next

            End If

            z = 0.99

            If mode = DirectX.DrawingMode.Normal Or mode = DirectX.DrawingMode.Line Or mode = DirectX.DrawingMode.Tinted Or mode = DirectX.DrawingMode.Text Then

                pY = offsetY
                currtiley = starttiley

                For tileY As Integer = 0 To DrawAreaTilesY
                    z -= 0.001
                    pX = offsetX
                    currtilex = starttilex
                    For tileX As Integer = 0 To DrawAreaTilesX

                        If mode = DirectX.DrawingMode.Normal Then

                            'bottom
                            tmps = mCurrentMap.GetTile(currtilex, currtiley)

                            If tmps.BottomTileSet <> 255 Then
                                If tmps.BottomTileIndex <> 65535 Then
                                    tmpImg = Libs.GetLib(GetImgLib(tmps.BottomTileSet)).Image(tmps.BottomTileIndex)
                                    If Not tmpImg Is Nothing Then
                                        With tmpImg

                                            tmpOffY = ((.Header.Height - TileHeight))
                                            DirectX.BlitD3D(.Texture, pX, pY - tmpOffY, .Header.Width, .Header.Height, z, arrZoom(ZoomIndex))

                                        End With
                                    End If
                                End If
                            End If


                            If tmps.TopTileSet <> 255 Then
                                If tmps.TopTileIndex <> 65535 Then
                                    tmpImg = Libs.GetLib(GetImgLib(tmps.TopTileSet)).Image(tmps.TopTileIndex)
                                    If Not tmpImg Is Nothing Then
                                        With tmpImg

                                            tmpOffY = ((.Header.Height - TileHeight))
                                            DirectX.BlitD3D(.Texture, pX, pY - tmpOffY, .Header.Width, .Header.Height, z, arrZoom(ZoomIndex))

                                        End With
                                    End If
                                End If
                            End If

                            'draw normal NPC (non selected)
                            NPC.DrawNPC(currtilex, currtiley, pX, pY, z)
                            Mobs.DrawMobs(mode, currtilex, currtiley, pX, pY, z)

                        ElseIf mode = DirectX.DrawingMode.Tinted Then

                            'NOTE: DRAW NPCS WITH DEFAULT RED TINT BEFORE OTHER TINTED OBJECTS

                            'draw selected NPC
                            'NPC.DrawNPC(currtilex, currtiley, pX, pY, z, True)

                        ElseIf mode = DirectX.DrawingMode.Text Then

                            Mobs.DrawMobs(mode, currtilex, currtiley, pX, pY, z)

                        ElseIf mode = DirectX.DrawingMode.Line Then

                            'check if grid should be visible
                            If GridVisible Then

                                'drawn grid:
                                'check if current X tile needs a grid line
                                Math.DivRem(currtilex, arrGrid(GridIndex), r)
                                If r = 0 Then
                                    'check if current Y tile needs a grid line
                                    Math.DivRem(currtiley, arrGrid(GridIndex), r)
                                    If r = 0 Then
                                        'draw grid lines X & Y
                                        Engine.DrawLine(pX * arrZoom(ZoomIndex), (pX + (TileWidth * arrGrid(GridIndex))) * arrZoom(ZoomIndex), pY * arrZoom(ZoomIndex), pY * arrZoom(ZoomIndex), 0.1F, 1, cGridColor)
                                        Engine.DrawLine(pX * arrZoom(ZoomIndex), pX * arrZoom(ZoomIndex), pY * arrZoom(ZoomIndex), (pY + (TileHeight * arrGrid(GridIndex))) * arrZoom(ZoomIndex), 0.1F, 1, cGridColor)
                                    End If
                                End If

                                'check zoom level
                                If ZoomIndex > 4 Then
                                    'draw centre crosshair
                                    Engine.DrawLine(winWidth / 2, winWidth / 2, (winHeight / 2) - 32, (winHeight / 2) + 32, 0.001F, 3, Color.Yellow)
                                    Engine.DrawLine((winWidth / 2) - 32, (winWidth / 2) + 32, winHeight / 2, winHeight / 2, 0.001F, 3, Color.Yellow)
                                End If

                            End If

                        End If

                        'If pX + (TileWidth * arrZoom(ZoomIndex)) = pX Then pX += 1 Else pX += (TileWidth * arrZoom(ZoomIndex))
                        pX += TileWidth
                        currtilex += 1
                    Next
                    'If pY + (TileHeight * arrZoom(ZoomIndex)) = pY Then pY += 1 Else pY += (TileHeight * arrZoom(ZoomIndex))
                    pY += TileHeight
                    currtiley += 1
                Next

            ElseIf mode = DirectX.DrawingMode.Text Then

                'draw debug text
                DirectX.ProFont9.DrawBorderText(Editor.DebugText(), 10, 30, cDebugTextColor, cDebugTextBdrColor, 0.01F)

                'draw bottom left info display
                DirectX.ProFont14.DrawBorderText(Editor.InfoText(), 10, winHeight - 24, cDebugTextColor, cDebugTextBdrColor, 0.01F)

            End If

        End Sub


        Public Sub SetDrawTint(c As Color, Optional blend As Single = 1.0F)

            DirectX.GameSprite.End()

            Engine.SetDrawModeTinted(blend, c)
            Windows.DrawWindows(DirectX.DrawingMode.Tinted)

            DirectX.GameSprite.Begin(Microsoft.DirectX.Direct3D.SpriteFlags.AlphaBlend)

        End Sub

        Public Sub KeyboardInput(ByRef type As Keyboard.KeyboardInputType, ByRef e As System.Windows.Forms.KeyEventArgs)

            If type = Keyboard.KeyboardInputType.KeyDown Then

                If e.KeyCode = Keys.Up Then
                    tmrMoveY.Enabled = True
                    tmrMoveY.Start()
                    MoveDistY = -1
                ElseIf e.KeyCode = Keys.Down Then
                    tmrMoveY.Enabled = True
                    tmrMoveY.Start()
                    MoveDistY = 1
                ElseIf e.KeyCode = Keys.Left Then
                    tmrMoveX.Enabled = True
                    tmrMoveX.Start()
                    MoveDistX = -1
                ElseIf e.KeyCode = Keys.Right Then
                    tmrMoveX.Enabled = True
                    tmrMoveX.Start()
                    MoveDistX = 1
                End If

            ElseIf type = Keyboard.KeyboardInputType.KeyUp Then

                If e.KeyCode = Keys.Up Then
                    tmrMoveY.Enabled = False
                    tmrMoveY.Stop()
                ElseIf e.KeyCode = Keys.Down Then
                    tmrMoveY.Enabled = False
                    tmrMoveY.Stop()
                ElseIf e.KeyCode = Keys.Left Then
                    tmrMoveX.Enabled = False
                    tmrMoveX.Stop()
                ElseIf e.KeyCode = Keys.Right Then
                    tmrMoveX.Enabled = False
                    tmrMoveX.Stop()
                End If

            End If

        End Sub

        Public Sub KeyboardInput(ByRef type As Keyboard.KeyboardInputType, ByRef e As System.Windows.Forms.KeyPressEventArgs)

            If type = Keyboard.KeyboardInputType.KeyPress Then

                Select Case LCase(e.KeyChar)

                    Case "-"
                        ZoomOut()

                    Case "+"
                        ZoomIn()

                    Case "["
                        MakeGridSmaller()

                    Case "]"
                        MakeGridLarger()

                End Select

            End If

        End Sub

        Private Sub ZoomOut()

            If ZoomIndex < 9 Then
                ZoomIndex += 1
                If ZoomIndex > 3 And GridIndex < 6 Then
                    GridIndex += 1
                End If
                CalculateDrawingArea()
            End If

        End Sub

        Private Sub ZoomIn()

            If ZoomIndex > 0 Then
                ZoomIndex -= 1
                If ZoomIndex < 6 And GridIndex > 0 Then
                    GridIndex -= 1
                End If
                CalculateDrawingArea()
            End If

        End Sub

        Private Sub MakeGridSmaller()

            If GridIndex > 0 Then
                GridIndex -= 1
            End If

        End Sub

        Private Sub MakeGridLarger()

            If GridIndex < 6 Then
                GridIndex += 1
            End If

        End Sub

        Private Sub CorrectCoords()

            Dim tilecountX As Integer = Int((winWidth / arrZoom(ZoomIndex) / 2) \ TileWidth)
            If tilecountX > (mCurrentMap.mWidth / 2) Then
                CoordX = mCurrentMap.mWidth / 2
            ElseIf CoordX - tilecountX < 0 - (10 / arrZoom(ZoomIndex)) Then
                CoordX = tilecountX - (10 / arrZoom(ZoomIndex))
            ElseIf CoordX + tilecountX > mCurrentMap.mWidth + (10 / arrZoom(ZoomIndex)) Then
                CoordX = mCurrentMap.mWidth - tilecountX + (10 / arrZoom(ZoomIndex))
            End If

            Dim tilecountY As Integer = Int((winHeight / arrZoom(ZoomIndex) / 2) \ TileHeight)
            If tilecountY > (mCurrentMap.mHeight / 2) Then
                CoordY = mCurrentMap.mHeight / 2
            ElseIf CoordY - tilecountY < 0 - (10 / arrZoom(ZoomIndex)) Then
                CoordY = tilecountY - (10 / arrZoom(ZoomIndex))
            ElseIf CoordY + tilecountY > mCurrentMap.mHeight + (10 / arrZoom(ZoomIndex)) Then
                CoordY = mCurrentMap.mHeight - tilecountY + (10 / arrZoom(ZoomIndex))
            End If

        End Sub

        Private Sub tmrMoveX_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrMoveX.Tick

            CoordX += (MoveDistX / arrZoom(ZoomIndex))
            CorrectCoords()

            'If starttilex < 0 Then starttilex = 0
            'If starttilex + Int(winWidth / (TileWidth * arrZoom(ZoomIndex))) > mCurrentMap.mWidth Then starttilex = mCurrentMap.mWidth - Int(winWidth / (TileWidth * arrZoom(ZoomIndex)))

            'If starttiley < 0 Then starttiley = 0
            'If starttiley + Int(winHeight / (TileHeight * arrZoom(ZoomIndex))) > mCurrentMap.mHeight Then starttiley = mCurrentMap.mHeight - Int(winHeight / (TileHeight * arrZoom(ZoomIndex)))


        End Sub

        Private Sub tmrMoveY_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrMoveY.Tick

            CoordY += (MoveDistY / arrZoom(ZoomIndex))
            CorrectCoords()

        End Sub

        Private Sub mWinEditor_MouseEvent(ByRef type As Mouse.mMouse.MouseInputType, ByRef e As System.Windows.Forms.MouseEventArgs) Handles mWinEditor.MouseEvent

            'Dim w As Integer, h As Integer

            'w = (winWidth * DrawAreaFactorX) / arrZoom(ZoomIndex)
            'h = (winHeight * DrawAreaFactorY) / arrZoom(ZoomIndex)

            'Dim r As Integer
            'DrawAreaTilesX = Math.DivRem(w, TileWidth, r)
            'If r = 0 Then DrawAreaTilesX += 1
            'DrawAreaTilesY = Math.DivRem(h, TileHeight, r)
            'If r = 0 Then DrawAreaTilesY += 1

            ''Make sure there is an odd number of tiles in each dimension so we get a centre tile for the player
            'Math.DivRem(DrawAreaTilesX, 2, r)
            'If r = 0 Then DrawAreaTilesX += 1
            'Math.DivRem(DrawAreaTilesY, 2, r)
            'If r = 0 Then DrawAreaTilesY += 1

            'Dim cx As Double = (winWidth / arrZoom(ZoomIndex) / 2) - (TileWidth / 2)
            'Dim cy As Double = (winHeight / arrZoom(ZoomIndex) / 2) - (TileHeight / 2)

            'DrawCentreTileX = DrawAreaTilesX * DrawAreaCentreX
            'DrawCentreTileY = DrawAreaTilesY * DrawAreaCentreY

            'DrawAreaStartOffsetX = cx - ((DrawCentreTileX) * TileWidth) '- Constants.TileWidth
            'DrawAreaStartOffsetY = cy - ((DrawCentreTileY) * TileHeight) '- Constants.TileHeight



            If type = Mouse.MouseInputType.MouseDown And e.Button = MouseButtons.Right Then

                Dim cx As Integer = (DrawAreaStartOffsetX + (DrawCentreTileX * (TileWidth)) + ((TileWidth) / 2))
                Dim cy As Integer = (DrawAreaStartOffsetY + (DrawCentreTileY * (TileHeight)) + ((TileHeight) / 2))

                tiley = (((e.Y / arrZoom(ZoomIndex)) - cy) / TileHeight) + CoordY
                tilex = (((e.X / arrZoom(ZoomIndex)) - cx) / TileWidth) + CoordX

                MenuTileX = tilex
                MenuTileY = tiley

                mWinEditorMenu.SetPosition(e.X + 6, e.Y + 6)

                SetupEditMenu()

                Windows.AddWindow(mWinEditorMenu, Windows.WindowType.AlwaysOnTop)

            ElseIf type = Mouse.MouseInputType.MouseDown And e.Button = MouseButtons.Left Then

                CloseEditMenu()

            End If

        End Sub

        Private Sub SetupEditMenu()

            'Get map element at current menu coords
            Dim CurrentMapElement As MapElement = GetMapElementType(MenuTileX, MenuTileY)

            'hide all menu buttons
            mBtnAddSpawn.Visible = False
            mBtnAddNPC.Visible = False
            mBtnAddSZ.Visible = False
            mBtnAddDoor.Visible = False

            mBtnEditSpawn.Visible = False
            mBtnEditNPC.Visible = False
            mBtnEditSZ.Visible = False
            mBtnEditDoor.Visible = False

            mBtnRemoveSpawn.Visible = False
            mBtnRemoveNPC.Visible = False
            mBtnRemoveSZ.Visible = False
            mBtnRemoveDoor.Visible = False

            mBtnToggleGrid.Visible = False
            mBtnToggleGrid2.Visible = False

            Select Case CurrentMapElement.T

                'npc
                Case MapElementType.NPC
                    SetMenuSizeEdit()
                    'set buttons to visible
                    mBtnEditNPC.Visible = True
                    mBtnRemoveNPC.Visible = True

                    'spawn
                Case MapElementType.Spawn
                    SetMenuSizeEdit()
                    'set buttons to visible
                    mBtnEditSpawn.Visible = True
                    mBtnRemoveSpawn.Visible = True

                    'safe zone
                Case MapElementType.SafeZone
                    SetMenuSizeEdit()
                    'set buttons to visible
                    mBtnEditSZ.Visible = True
                    mBtnRemoveSZ.Visible = True

                    'door
                Case MapElementType.Door
                    SetMenuSizeEdit()
                    'set buttons to visible
                    mBtnEditDoor.Visible = True
                    mBtnRemoveDoor.Visible = True

                    'none / default
                Case Else
                    'set size
                    mWinEditorMenu.SetSize(128, 80)
                    'set buttons to visible
                    mBtnToggleGrid.Visible = True
                    mBtnAddSpawn.Visible = True
                    mBtnAddNPC.Visible = True
                    mBtnAddSZ.Visible = True
                    mBtnAddDoor.Visible = True
                    mBtnToggleGrid.Visible = True

            End Select

        End Sub

        Private Sub SetMenuSizeEdit()
            'set window size
            mWinEditorMenu.SetSize(128, 48)
            'make 2nd toggle grid button to visible
            mBtnToggleGrid2.Visible = True
        End Sub

        Private Sub CloseEditMenu()

            Windows.RemoveWindow(mWinEditorMenu)
            Windows.FocusWindow(mWinEditor)

        End Sub

        Private Sub mBtnToggleGrid_Click() Handles mBtnToggleGrid.Click
            ToggleGrid()
        End Sub

        Private Sub mBtnToggleGrid2_Click() Handles mBtnToggleGrid2.Click
            ToggleGrid()
        End Sub

        Private Sub ToggleGrid()

            'toggle grid visible flag
            GridVisible = Not GridVisible

            CloseEditMenu()

        End Sub

        Private Sub mBtnZoomIn_Click() Handles mBtnZoomIn.Click
            ZoomIn()
        End Sub

        Private Sub mBtnZoomOut_Click() Handles mBtnZoomOut.Click
            ZoomOut()
        End Sub

        Private Sub mBtnToggleSymbols_Click() Handles mBtnToggleSymbols.Click

            bShowSymbols = Not bShowSymbols

            If bShowSymbols Then
                mBtnToggleSymbols.Text = "ON"
            Else
                mBtnToggleSymbols.Text = ""
            End If

        End Sub

        Private Sub mBtnAddSZ_Click() Handles mBtnAddSZ.Click

        End Sub

        Private Sub mBtnAddSpawn_Click() Handles mBtnAddSpawn.Click

            If Main.fAddSpawn Is Nothing Then

                Main.fAddSpawn = New fAddMob
                Main.fAddSpawn.Init()

            End If

            Main.fAddSpawn.ShowAddMob(MenuTileX, MenuTileY, mCurrentMap.mWidth, mCurrentMap.mHeight, mCurrentMapFilename)

            CloseEditMenu()

        End Sub

        Private Sub mBtnRemoveSpawn_Click() Handles mBtnRemoveSpawn.Click

            Editor.Mobs.RemoveSpawn(MenuTileX, MenuTileY)

            CloseEditMenu()

        End Sub
    End Module

End Namespace